﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.IO;
using System.Net.Mail;


namespace fgbt_move
{
    class Program
    {
        static String seedloc = @"C:\Users\Administrator\Desktop\Move";
        static int limit = 2;
//        static long size = 50000000000;
        static String SAS = @"I:\";
        static StreamWriter sw;

        static void Main(string[] args)
        {
            FileStream fs = new FileStream("log"+DateTime.Now.ToString("yyyy-MM-dd")+".txt", FileMode.Create);
            sw = new StreamWriter(fs);
            disk i=new disk(@"I:\");
            while (i.getFreeSize() < 45000000000)
            {
                UTControl.SetUTPause("http://127.0.0.1:62956/", "admin", "keeper");
                int moved = 0;
                if (seedloc.Length > 0)
                {
                    DirectoryInfo df = new DirectoryInfo(seedloc);
                    DateTime dt = DateTime.Now;
                    foreach (FileInfo fl in df.GetFiles())
                    {//遍历下载完成的那个目录 出现了完成时间超过阀值的 便移动
                        if (dt.Subtract(fl.CreationTime).Duration().Days > limit)
                            try
                            {
                                moved++;
                                System.Console.WriteLine("处理种子文件" + fl.Name + "\r\n");
                                disk d = getLocate();
                                Torrent t = new Torrent(fl.FullName);
                                if (t.FileList.Count > 0)
                                {
                                    long le = 0;
                                    foreach (Torrent.FileInfo filemove in t.FileList)
                                    {
                                        le += filemove.Length;
                                    }
                                    d.clean(le + 10000000000);
                                    CopyDirectory(SAS + t.Name, d.getname() + t.Name, true);
                                }
                                else
                                {
                                    System.Console.WriteLine("复制单一文件从：" + SAS + fl.Name.Replace(".torrent", "") + " 至：" + d.getname() + fl.Name.Replace(".torrent", "") + "\r\n");
                                    sw.WriteLine("复制单一文件从：" + SAS + fl.Name.Replace(".torrent", "") + " 至：" + d.getname() + fl.Name.Replace(".torrent", "") + "\r\n");
                                    FileInfo f = new FileInfo(SAS + fl.Name.Replace(".torrent", ""));
                                    d.clean(f.Length + 10000000000);
                                    File.Copy(SAS + fl.Name.Replace(".torrent", ""), d.getname() + fl.Name.Replace(".torrent", ""), true);

                                }
                                Random rd = new Random();
                                int k = rd.Next(1, 100);
                                if (k <= 33)
                                {
                                    UTControl.SetUTSaveDir("http://127.0.0.1:23484/", "admin", "keeper", d.getname());
                                    File.Move(fl.FullName, @"seed\" + fl.Name);
                                }
                                else if (k <= 66)
                                {
                                    UTControl.SetUTSaveDir("http://127.0.0.1:12841/", "admin", "keeper", d.getname());
                                    File.Move(fl.FullName, @"seed-1\" + fl.Name);
                                }
                                else
                                {
                                    UTControl.SetUTSaveDir("http://127.0.0.1:41201/", "admin", "keeper", d.getname());
                                    File.Move(fl.FullName, @"seed-2\" + fl.Name);
                                }

                                //UTControl.SetUTSaveDir("http://127.0.0.1:12841/", "admin", "keeper", d.getname());
                                //File.Move(fl.FullName, @"seed-1\" + fl.Name);

                                if (t.FileList.Count > 0)
                                {
//                                    fl.Delete();
                                    DirectoryInfo di = new DirectoryInfo(SAS + t.Name);
                                    di.Delete(true);
                                }
                                else
                                {
//                                    fl.Delete();
                                    File.Delete(SAS + fl.Name.Replace(".torrent", ""));
                                }

                            }
                            catch (Exception e)
                            {
                                System.Console.WriteLine("出现错误：" + e + "\r\n");
                                sw.WriteLine("出现错误：" + e + "\r\n");
                            }
                        if (i.getFreeSize() > 100000000000)
                            break;
                    }
                    if ((moved == 0)&&(limit>0))
                        limit--;
                }
            }
            UTControl.SetUTSpeed("http://127.0.0.1:62956/", "admin", "keeper",0,0);
            sw.Close();
            fs.Close();
        }

        private static bool CopyDirectory(string SourcePath, string DestinationPath, bool overwriteexisting)
        {
            bool ret = false;
            try
            {
                SourcePath = SourcePath.EndsWith(@"\") ? SourcePath : SourcePath + @"\";
                DestinationPath = DestinationPath.EndsWith(@"\") ? DestinationPath : DestinationPath + @"\";

                if (Directory.Exists(SourcePath))
                {
                    if (Directory.Exists(DestinationPath) == false)
                        Directory.CreateDirectory(DestinationPath);

                    foreach (string fls in Directory.GetFiles(SourcePath))
                    {
                        FileInfo flinfo = new FileInfo(fls);
                        System.Console.WriteLine("复制文件 至：" + DestinationPath + flinfo.Name +" Size:"+flinfo.Length/1024/1024 + "MB \r\n");
                        sw.WriteLine("复制文件 至：" + DestinationPath + flinfo.Name + "\r\n");
                        flinfo.CopyTo(DestinationPath + flinfo.Name, overwriteexisting);
                    }
                    foreach (string drs in Directory.GetDirectories(SourcePath))
                    {
                        DirectoryInfo drinfo = new DirectoryInfo(drs);
                        if (CopyDirectory(drs, DestinationPath + drinfo.Name, overwriteexisting) == false)
                            ret = false;
                    }
                }
                ret = true;
            }
            catch (Exception ex)
            {
                System.Console.WriteLine("出现错误：" + ex + "\r\n");
                sw.WriteLine("出现错误：" + ex + "\r\n");
                ret = false;
            }
            return ret;
        } 

        static private disk getLocate()
        {
            StreamReader sr = new StreamReader("config.txt");
            int num = int.Parse(sr.ReadLine());
            disk ma = null;

            Random ro = new Random(); 
            int iResult;
            iResult = ro.Next(1,num+1);

            for (int i = 1; i <= iResult; i++)
            {
                disk d = new disk(sr.ReadLine());
                ma = d;
            }
            //long free = 0;
            //for (int i = 1; i <= num; i++)
            //{
            //    disk d = new disk(sr.ReadLine());
            //    if (d.getFreeSize() > free)
            //    {
            //        free = d.getFreeSize();
            //        ma = d;
            //    }
            //}

            sr.Close();
            return ma;
        }

    }
}
